package com.ruoyi.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.course.domain.ExamCourse;
import com.ruoyi.course.domain.ExamCourseTagRelation;
import com.ruoyi.course.domain.ExamCourseBank;
import com.ruoyi.course.vo.ExamCourseVo;
import com.ruoyi.course.vo.ExamCourseTagRelationVo;
import com.ruoyi.course.vo.ExamCourseBankDetailVo;
import com.ruoyi.course.mapper.ExamCourseMapper;
import com.ruoyi.course.mapper.ExamCourseTagRelationMapper;
import com.ruoyi.course.mapper.ExamCourseBankMapper;
import com.ruoyi.course.service.IExamCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 课程信息Service业务层处理
 *
 * @author Tellsea
 * @date 2025-09-25
 */
@Service
public class ExamCourseServiceImpl extends ServiceImpl<ExamCourseMapper, ExamCourse> implements IExamCourseService {

    @Autowired
    private ExamCourseTagRelationMapper examCourseTagRelationMapper;

    @Autowired
    private ExamCourseBankMapper examCourseBankMapper;

    @Override
    public TableDataInfo<ExamCourseVo> queryList(ExamCourseVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryList(PageUtils.buildPage(), entity));
    }

    @Override
    public List<ExamCourseVo> queryAll(ExamCourseVo entity) {
        return this.baseMapper.queryList(entity);
    }

    @Override
    public ExamCourseVo queryById(Long id) {
        return this.baseMapper.queryById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveTagRelation(ExamCourseTagRelationVo entity) {
        if (entity.getCourseId() == null || entity.getTagIds() == null || entity.getTagIds().isEmpty()) {
            return false;
        }
        // 在批量插入标签之前，清空之前的所有关系数据
        examCourseTagRelationMapper.delete(new QueryWrapper<ExamCourseTagRelation>().eq("course_id", entity.getCourseId()));
        
        // 批量插入课程标签关系
        int result = examCourseTagRelationMapper.batchInsertTagRelations(entity.getCourseId(), entity.getTagIds());
        return result > 0;
    }

    @Override
    public TableDataInfo<ExamCourseBankDetailVo> queryCourseBankList(ExamCourseBankDetailVo entity) {
        return PageUtils.buildDataInfo(this.baseMapper.queryCourseBankList(PageUtils.buildPage(), entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addCourseBanks(Long courseId, List<Long> bankIds) {
        if (bankIds == null || bankIds.isEmpty()) {
            return 0;
        }

        int successCount = 0;
        for (Long bankId : bankIds) {
            // 检查关系是否已存在
            QueryWrapper<ExamCourseBank> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("course_id", courseId).eq("bank_id", bankId);
            
            if (examCourseBankMapper.selectCount(queryWrapper) > 0) {
                // 关系已存在，跳过
                continue;
            }
            
            // 创建新的课程题库关系
            ExamCourseBank examCourseBank = new ExamCourseBank();
            examCourseBank.setCourseId(courseId);
            examCourseBank.setBankId(bankId);
            
            int result = examCourseBankMapper.insert(examCourseBank);
            if (result > 0) {
                successCount++;
            }
        }

        return successCount;
    }

    @Override
    public int removeCourseBank(Long courseId, Long bankId) {
        QueryWrapper<ExamCourseBank> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id", courseId).eq("bank_id", bankId);
        return examCourseBankMapper.delete(queryWrapper);
    }
}
